package com.hyh.baowenlioaworking.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyh.baowenlioaworking.commom.Constants;
import com.hyh.baowenlioaworking.commom.R;
import com.hyh.baowenlioaworking.mapper.PositionMapper;
import com.hyh.baowenlioaworking.model.Position;
import com.hyh.baowenlioaworking.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.service.impl
 * @ClassName PositionServiceImpl
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  14:06
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    private PositionMapper positionMapper;

    /**
     * 查询所有信息
     *
     * @return
     */
    @Override
    public List<Position> findAll() {
        return positionMapper.findAll();
    }
    @Override
    public IPage<Position> findAll(Page<Position> page) {
        return positionMapper.findAll(page);
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @Override
    public Position findById(int id) {
        if (id==0){
            R.error(Constants.CODE_405,"id错误！！");
        }else if (id<=0){
            R.error(Constants.CODE_600, "id必须大于0");
        }else {
            Position position = positionMapper.findById(id);
            return position;
        }
        return positionMapper.findById(id);
    }

    /**
     * 删除数据，根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public R<Integer> deleteById(int id) {
        if (id == 0) {
            return R.error(Constants.CODE_600, "id不能为0");
        } else {
            Integer res = positionMapper.deleteById(id);
            if (res != 0) {
                return R.success(Constants.CODE_200, "删除成功！！！");
            } else {
                return R.error(Constants.CODE_404, "id没有找到！！！");
            }
        }
    }

    /**
     * 修改数据，根据id修改id下的数据
     *
     * @param position
     * @return
     */
    @Override
    public int updatePosition(Position position) {
        Integer res = positionMapper.updatePosition(position);
        if (res != 0) {
             R.success(Constants.CODE_200, "修改成功");
        }
        R.error(Constants.CODE_400, "修改失败");
        return res;
    }

    /**
     * 添加数据
     * @param position
     * @return
     */
    @Override
    public R<String> addPosition(Position position) {
        String name = position.getName();
        if (name == null || name.equals("") || name.length() != 0) {
            int i=positionMapper.addPosition(position);
            if (i!=0){
                return R.success(Constants.CODE_200,"添加成功");

            }
        }
        return R.error(Constants.CODE_405,"添加失败！！！");
    }


    /**
     * 获得数据的总数
     * @return
     */
    @Override
    public int getTotal() {
        return positionMapper.getTotalCount();
    }


}
